import { Message } from 'view-design'
import dayjs from 'dayjs'
import { assign, head } from 'lodash'
import {
  fetchTableData,
  fetchTreeMap,
  fetchCenterTopCandData,
  fetchMeterDetail,
  fetchAbnormalData,
  fetchSetParams,
  fetchGetParams,
  fetchXr
} from '@/api/smallGas'

const defaultState = () => {
  return {
    defaultDate: undefined,
    tablePageParams: {
      pageNum: 1,
      pageSize: 20
    },
    pageTotal: 0,
    tableLoading: false,
    tableData: null,
    stopTableKeyUp: false,
    currentMeterId: undefined,
    currentGasCls: undefined,
    treeMapList: [],
    centerTopCandList: [],
    centerTopChartLoading: false,
    collapseLeftFlag: false,
    collapseRightFlag: false,
    meterDetail: null,
    startTime: undefined,
    endTime: undefined,
    abnormalList: [],
    errorType: 0,
    currentClickTableDate: undefined,
    ycsytLoading: false
  }
}

export default {
  namespaced: true,
  name: 'smallGas',
  state: defaultState(),
  getters: {
    getTablePageParams: state => {
      return {
        ...state.tablePageParams
      }
    }
  },
  mutations: {
    REST_STATE(state) {
      state = assign(state, defaultState())
    },
    /**
     * desc: 设置表格数据
     * param {type}
     * return {type} void
     */
    SET_TABLE_DATA(state, tableData) {
      state.tableData = tableData
    },
    SET_TABLE_LOADING(state, flag) {
      state.tableLoading = flag
    },
    /**
     * desc: 设置表格键盘操作标识符
     * param {type}
     * return {type} void
     */
    SET_STOP_TABLE_KEYUP(state, flag) {
      state.stopTableKeyUp = flag
    },
    SET_CURRENT_METER_ID(state, meterId) {
      state.currentMeterId = meterId
    },
    /**
     * desc: 设置表格请求页数
     * param {type}
     * return {type} void
     */
    SET_TABLE_PAGE_NUM(state, num) {
      state.tablePageParams.pageNum = num
    },
    /**
     * desc: 设置矩形树图数据
     * param {type}
     * return {type} void
     */
    SET_TREEMAP_DATA(state, data) {
      state.treeMapList = data
    },
    SET_DEFAULT_DATE(state, date) {
      state.defaultDate = date
    },
    SET_CENTER_TOP_CHART_LOADING(state, flag) {
      state.centerTopChartLoading = flag
    },
    SET_CENTER_TOP_CANDDATA(state, data) {
      state.centerTopCandList = data
    },
    SET_COLLAPSE(state, { type, status }) {
      if (type === 'left') {
        state.collapseLeftFlag = status
      } else if (type === 'right') {
        state.collapseRightFlag = status
      }
    },
    SET_METER_DETAIL(state, data) {
      state.meterDetail = data
    },
    SET_START_AND_END_TIME(state, dates) {
      const [startTime, endTime] = dates
      state.startTime = startTime || undefined
      state.endTime = endTime || undefined
    },
    SET_CURRENT_METERID(state, meterId) {
      state.currentMeterId = meterId
    },
    SET_ABNORMAL_DATA(state, data) {
      state.abnormalList = data
    },
    SET_ERROR_TYPE(state, type) {
      state.errorType = type
    },
    SET_CURRENT_CLICK_TABLE_DATE(state, date) {
      state.currentClickTableDate = date
    },
    SET_CURRENT_GAS_CLs(state, type) {
      state.currentGasCls = type
    },
    SET_YCSYT_LOADING(state, flag) {
      state.ycsytLoading = flag
    }
  },
  actions: {
    clearState(ctx) {
      ctx.commit('REST_STATE')
    },
    async loadTableData(ctx, params) {
      ctx.commit('SET_TABLE_LOADING', true)
      const { code, date } = await fetchTableData({
        negAlyType: ctx.state.errorType,
        ...ctx.state.tablePageParams,
        ...params
      })
      ctx.commit('SET_TABLE_LOADING', false)
      if (code === 200) {
        ctx.commit('SET_TABLE_DATA', date)
      }
    },
    /**
     * desc: 是否允许表格键盘操作
     * param {type}
     * return {type} void
     */
    changeStopTableKeyUp(ctx, flag) {
      ctx.commit('SET_STOP_TABLE_KEYUP', flag)
    },
    changeMeterId(ctx, meterId) {
      ctx.commit('SET_CURRENT_METER_ID', meterId)
    },
    /**
     * desc: 修改表格页码
     * param {type}
     * return {type} void
     */
    changeTablePageNum(ctx, num) {
      ctx.commit('SET_TABLE_PAGE_NUM', num)
    },
    /**
     * desc: 获取矩形树图
     * param {type}
     * return {type} void
     */
    async loadTreeMapData(ctx) {
      const { code, date } = await fetchTreeMap()
      if (code === 200) {
        ctx.commit('SET_TREEMAP_DATA', date)
      }
    },
    async loadCenterTopCandData(ctx, params = {}) {
      ctx.commit('SET_ABNORMAL_DATA', [])
      ctx.commit('SET_CENTER_TOP_CHART_LOADING', true)
      const { code, date } = await fetchCenterTopCandData({
        ...params,
        meterId: ctx.state.currentMeterId
      })
      if (date.length > 0) {
        const _oldDate = head(date) ? head(date)[0] : undefined
        const _newDate =
          _oldDate &&
          dayjs(_oldDate)
            .subtract(1, 'days')
            .format('YYYY-MM-DD')
        ctx.commit('SET_DEFAULT_DATE', _newDate)
      }
      ctx.commit('SET_CENTER_TOP_CHART_LOADING', false)
      if (code === 200) {
        if (date.length > 0) {
          const _list = [...date, ...ctx.state.centerTopCandList]
          ctx.commit('SET_CENTER_TOP_CANDDATA', _list)
        } else {
          Message.warning('没有可加载的数据了')
        }
      }
    },
    changeCollapse(ctx, arg) {
      ctx.commit('SET_COLLAPSE', arg)
    },
    clearCenterTopCandData(ctx) {
      ctx.commit('SET_CENTER_TOP_CANDDATA', [])
    },
    async lodaMeterDetail(ctx) {
      const { code, date } = await fetchMeterDetail(ctx.state.currentMeterId)
      if (code === 200) {
        ctx.commit('SET_METER_DETAIL', date)
      }
    },
    changeStartAndEndTime(ctx, dates) {
      ctx.commit('SET_START_AND_END_TIME', dates)
    },
    changeDefaultDate(ctx, date) {
      ctx.commit('SET_DEFAULT_DATE', date)
    },
    changeCurrentMeterId(ctx, meterId) {
      ctx.commit('SET_CURRENT_METERID', meterId)
    },
    async loadAbnormalData(ctx, params) {
      ctx.commit('SET_YCSYT_LOADING', true)
      const { code, date } = await fetchAbnormalData({
        meterId: ctx.state.currentMeterId,
        ...params
      })
      ctx.commit('SET_YCSYT_LOADING', false)
      if (code === 200) {
        ctx.commit('SET_ABNORMAL_DATA', date)
      }
    },
    changeErrorType(ctx, type) {
      ctx.commit('SET_ERROR_TYPE', type)
    },
    changeCurrentClickTableDate(ctx, date) {
      ctx.commit('SET_CURRENT_CLICK_TABLE_DATE', date)
    },
    changeCurrentGasCls(ctx, type) {
      ctx.commit('SET_CURRENT_GAS_CLs', type)
    },
    async loadSetParams(ctx, data) {
      const _ret = await fetchSetParams(data)
      return _ret
    },
    async loadGetParams(ctx, data) {
      const _ret = await fetchGetParams(data)
      return _ret
    },
    async loadXr(ctx, data) {
      const _ret = await fetchXr({
        meterId: ctx.state.currentMeterId,
        ...data
      })
      return _ret
    }
  }
}
